package com.mai4j.maiadmin.support.security;

import com.mai4j.maiadmin.support.cache.Session;
import org.springframework.security.authentication.AuthenticationTrustResolver;
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Set;

/**
 * 微信公众号请关注: 开源螺丝钉
 * <br>
 * 码云请关注: <a href="https://gitee.com/xiyoufang">https://gitee.com/xiyoufang</a>
 * <br>
 * 哔哩哔哩请关注: <a href="https://space.bilibili.com/438927834">https://space.bilibili.com/438927834</a>
 *
 * @author xiyoufang
 */
public class SecurityUtils {

    private static final AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl();

    /**
     * 进入登录状态
     *
     * @param jwt       jwt
     * @param principal principal
     */
    public static void login(String jwt, Principal principal, Set<String> authorities) {
        JwtAuthentication authentication = new JwtAuthentication(principal, jwt,
                AuthorityUtils.createAuthorityList(authorities.toArray(new String[]{})));
        SecurityContextHolder.getContext().setAuthentication(authentication); // 添加信息到redis
        Session.put();
    }

    /**
     * 注销
     */
    public static void logout() {
        Session.remove();
    }

    /**
     * 获取  Authentication
     *
     * @return Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * @return authoritySet
     */
    public static Set<String> getAuthoritySet() {
        return AuthorityUtils.authorityListToSet(SecurityContextHolder.getContext().getAuthentication().getAuthorities());
    }

    /**
     * 校验器
     */
    public static class Helper {

        /**
         * 是否授权
         *
         * @return boolean
         */
        public static boolean isAuthenticated() {
            return !trustResolver.isAnonymous(getAuthentication());
        }

        /**
         * 检查权限
         *
         * @param authority authority
         * @return boolean
         */
        public static boolean hasAuthority(String authority) {
            return getAuthoritySet().contains(authority);
        }

        /**
         * 有任意一个权限
         *
         * @param authorities authorities
         * @return boolean
         */
        public static boolean hasAnyAuthority(String... authorities) {
            Set<String> authoritySet = getAuthoritySet();
            for (String authority : authorities) {
                if (authoritySet.contains(authority)) {
                    return true;
                }
            }
            return false;
        }

        /**
         * 登录用户的信息
         *
         * @return Principal
         */
        public static Principal principal() {
            return (Principal) getAuthentication().getPrincipal();
        }

        /**
         * 有任意所以权限
         *
         * @param authorities authorities
         * @return boolean
         */
        public static boolean hasAllAuthority(String... authorities) {
            Set<String> authoritySet = getAuthoritySet();
            for (String authority : authorities) {
                if (!authoritySet.contains(authority)) {
                    return false;
                }
            }
            return true;
        }
    }
}
